//日志标签定义
#define LOG_TAG     "thread"
#include <COMMON_HEADER/common_header.h>
/*
 * Copyright (c) 2016-2020, Chengdu RST technology co., ltd
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-01-05     shijiabao       the first version
 */

//注册包
char registration_package[0x20] = { 0 };
extern uint8_t led_network_status;
extern uint8_t led_connect_status;
uint8_t connnect_number = 0;
/* 线程 1 的入口函数 */
void watchdog_thread(void *parameter)
{
//    const char test_char[] = "datacacheceshi";
//    char *read_data_cache = NULL;
    while (1)
    {
        rt_thread_mdelay(10000);
        feed_dog();

//        if(!data_cache_write(test_char))
//        {
//            LOG_I("data cache write success!");
//        }
//        else
//        {
//            LOG_E("data cache write failed!");
//        }
//        rt_thread_mdelay(1000);
//
//        if((read_data_cache = data_cache_read())!=NULL)
//        {
//            LOG_I("data cache read succes!:%s",read_data_cache);
//            free(read_data_cache);
//        }
//        else
//        {
//            LOG_I("data cache read failed!");
//            free(read_data_cache);
//        }
        //日志清理
        //rt_kprintf("test\n");
        clear_redundant_ulog_file();
    }
}

void heartbeat_thread_entry(void *parameter)
{
    char heartbeat_backpackge[0x20] = { 0 };
    char heartbeat_time[2] = { 0 };
    int heartbeat_time_int = 0;

    if (chip_flash_configuretion_read(BREATHE_PACKET_ADDR, heartbeat_backpackge))
    {
        LOG_I("心跳包读取成功:%s", heartbeat_backpackge);
    }
    else
    {
        LOG_E("心跳包读取失败:%s", heartbeat_backpackge);
    }

    if (chip_flash_configuretion_read(BREATHE_TIME_ADDR, heartbeat_time))
    {
        heartbeat_time_int = (heartbeat_time[0] + heartbeat_time[1] * 256) * 1000;
        LOG_I("心跳包间隔读取成功:%d", heartbeat_time[0] + heartbeat_time[1] * 256);
    }
    else
    {
        heartbeat_time_int = 10000;
        LOG_E("心跳包间隔读取失败:%s", heartbeat_time[0] + heartbeat_time[1] * 256);
    }

    rt_thread_mdelay(10000);

    while (1)
    {
        if (lte_data.channel_connect_flag[1] == 1)
        {
            if (!lte_channel_send_data(1, heartbeat_backpackge, strlen(heartbeat_backpackge)))
            {
                LOG_I("透传中心1上传心跳包成功");
            }
            else
            {
                lte_data.channel_connect_flag[1] = 0;
            }
        }
        rt_thread_mdelay(heartbeat_time_int / 2);
        if (lte_data.channel_connect_flag[2] == 1)
        {
            if (!lte_channel_send_data(2, heartbeat_backpackge, strlen(heartbeat_backpackge)))
            {
                LOG_I("透传中心2上传心跳包成功");
            }
            else
            {
                lte_data.channel_connect_flag[2] = 0;
            }
        }
        rt_thread_mdelay(heartbeat_time_int / 2);
    }
}

/**
 * @brief 网络连接线程，负责建立与服务器的连接，当发生断线时，保持重连，如果多次重连未成功，则软复位网络模块，再重新连接，以此循环
 * @author shijiabao
 * @date 2021.08.19 rewrite
 * @param
 * @return
 */
void network_connect_thread(void *parameter)
{
    int network_init_freq = 0;

    while (1)
    {
        network_init_start:
        /*Initialize the connection status of transparent transmission center 1, 2 and private datacenter to - 1*/
        dn400_parm.dn400_status.transparent_transmission_server1_connect_flag = -1;
        dn400_parm.dn400_status.transparent_transmission_server2_connect_flag = -1;
        dn400_parm.dn400_status.private_server_connect_flag = -1;
        network_init_freq++;
        /* More than 5 reconnections,device reboot */
        if (network_init_freq > 6)
        {
            /* reboot */
            rt_hw_interrupt_disable();
            HAL_NVIC_SystemReset();
        }

        /*connect status led init(power off);network regesiter led init(power off)*/
        led_connect_status_change(0);
        led_network_status_change(0);

        if (sim76xx_device_register() == 0)
        {
            LOG_I("network init successed.\n");
            led_network_status_change(1);
        }
        else
        {
            goto network_init_start;
        }

        if (strstr(dn400_parm.dn400_configure.data_transmission_mode, "1") != NULL)
        {
            /* first connect */
            dn400_parm.tcpclient1 = rt_tc_test_init(1, dn400_parm.dn400_configure.data_center1_ip,
                                                    atoi(dn400_parm.dn400_configure.data_center1_port));
            dn400_parm.tcpclient2 = rt_tc_test_init(2,dn400_parm.dn400_configure.data_center2_ip,atoi(dn400_parm.dn400_configure.data_center2_port));
            rt_thread_mdelay(20000);
            /* reconnect */
            while(1)
            {
                //LOG_D("Datacenter1 connect flag:%d\n",dn400_parm.dn400_status.transparent_transmission_server1_connect_flag);
                //LOG_D("Datacenter2 connect flag:%d\n",dn400_parm.dn400_status.transparent_transmission_server2_connect_flag);

//                if(dn400_parm.dn400_status.transparent_transmission_server1_connect_flag == -1 && dn400_parm.dn400_status.transparent_transmission_server2_connect_flag == -1)
//                {
//                    break;
//                }
//                if(dn400_parm.dn400_status.transparent_transmission_server1_connect_flag == -1)
//                {
//                    LOG_I("Datacenter1 reconnect.\n");
//                    dn400_parm.tcpclient1 = rt_tcpclient_start(1, dn400_parm.dn400_configure.data_center1_ip,
//                                        atoi(dn400_parm.dn400_configure.data_center1_port));
//                }
//                if(dn400_parm.dn400_status.transparent_transmission_server2_connect_flag == -1)
//                {
//                    LOG_I("Datacenter2 reconnect.\n");
//                    dn400_parm.tcpclient2 = rt_tcpclient_start(2,dn400_parm.dn400_configure.data_center2_ip,atoi(dn400_parm.dn400_configure.data_center2_port));
//                }
                rt_thread_mdelay(1000);
            }
        }
        else if (strstr(dn400_parm.dn400_configure.data_transmission_mode, "2") != NULL)
        {
            ;
            //all_private_transmission_mode_start();
        }
        else
        {
            LOG_D("data transport mode parameter error or unknown.");
        }
        rt_thread_mdelay(100000000);
    }
}

/* 线程 7的入口函数 */
void connect_led_thread_entry(void *parameter)
{
    rt_thread_mdelay(8000);
    while (1)
    {
        //0灭
        if (led_connect_status == 0)
        {
            led1_down();
            rt_thread_mdelay(200);
        }
        //1亮
        if (led_connect_status == 1)
        {
            led1_on();
            rt_thread_mdelay(200);
        }
        //2 间隔2s慢闪
        if (led_connect_status == 2)
        {
            led1_on();
            rt_thread_mdelay(1000);
            led1_down();
            rt_thread_mdelay(1000);
        }
        //3 升级时最快的闪动
        if (led_connect_status == 3)
        {
            led1_on();
            rt_thread_mdelay(100);
            led1_down();
            rt_thread_mdelay(100);
        }
    }
}

/* 线程 7的入口函数 */
void network_led_thread_entry(void *parameter)
{
    rt_thread_mdelay(8000);
    while (1)
    {
        //0灭
        if (led_network_status == 0)
        {
            led3_down();
            rt_thread_mdelay(100);
        }
        //1亮
        if (led_network_status == 1)
        {
            led3_on();
            rt_thread_mdelay(100);
        }
        //2 间隔300ms快闪
        if (led_network_status == 2)
        {
            led3_on();
            rt_thread_mdelay(300);
            led3_down();
            rt_thread_mdelay(300);
        }
        //3 升级时最快的闪动
        if (led_network_status == 3)
        {
            led3_on();
            rt_thread_mdelay(100);
            led3_down();
            rt_thread_mdelay(100);
        }
    }
}

//socket数据异步接收线程1，对应透传1接口
void socket_recv_thread1_entry(void)
{
    uint8_t channel1_using_flag = 0;
    int recv_data_size = 0;

    //检查通道1是否生效
    if (strstr(dn400_parm.dn400_configure.data_center1_using_flag, "ON") != NULL)
    {
        channel1_using_flag = 1;
    }

    //网络传输模式1或者网络传输模式3
    if ((strstr(dn400_parm.dn400_configure.data_transmission_mode, "1") != NULL)
            || (strstr(dn400_parm.dn400_configure.data_transmission_mode, "3") != NULL))
    {
        if (channel1_using_flag == 1)
        {
            while (1)
            {
                rt_thread_mdelay(100);
                if (dn400_parm.dn400_status.transparent_transmission_server1_connect_flag == 1)
                {
                    memset(sock_recv_data_buff, 0, sizeof(sock_recv_data_buff));
                    recv_data_size = tcpclient_recv_data(dn400_parm.dn400_status.transparent_transmission1_sockfd,
                            sock_recv_data_buff, sizeof(sock_recv_data_buff));
                    if (recv_data_size > 0)
                    {
                        //成功接收到数据
                        LOG_D("datacenter1 recv data:%s", sock_recv_data_buff);
                        rs232_uart_write(sock_recv_data_buff, recv_data_size);
                        rs485_uart_write(sock_recv_data_buff, recv_data_size);
                    }
                    else if (recv_data_size == 0)
                    {
                        LOG_D("datacenter1 recv data 0 bit.");
                        LOG_I("datacenter1 disconnect.");
                        dn400_parm.dn400_status.transparent_transmission_server1_connect_flag = 0;
                    }
                    else
                    {
                        LOG_D("datacenter1 recv data error!");
                        LOG_I("datacenter1 disconnect.");
                        dn400_parm.dn400_status.transparent_transmission_server1_connect_flag = 0;
                    }
                }
            }
        }
    }
}

//socket数据异步接收线程2，对应透传2接口
void socket_recv_thread2_entry(void)
{
    uint8_t channel2_using_flag = 0;
    int recv_data_size = 0;

    //检查通道2是否生效
    if (strstr(dn400_parm.dn400_configure.data_center2_using_flag, "ON") != NULL)
    {
        channel2_using_flag = 1;
    }

    //网络传输模式1或者网络传输模式3
    if ((strstr(dn400_parm.dn400_configure.data_transmission_mode, "1") != NULL)
            || (strstr(dn400_parm.dn400_configure.data_transmission_mode, "3") != NULL))
    {
        if (channel2_using_flag == 1)
        {
            while (1)
            {
                rt_thread_mdelay(100);
                //LOG_D("***************recv data***************");
//                if (dn400_parm.dn400_status.transparent_transmission_server2_connect_flag == 1)
//                {
//                    memset(sock_recv_data_buff, 0, sizeof(sock_recv_data_buff));
//                    recv_data_size = tcpclient_recv_data(dn400_parm.dn400_status.transparent_transmission2_sockfd,
//                            sock_recv_data_buff, sizeof(sock_recv_data_buff));
//                    if (recv_data_size > 0)
//                    {
//                        //成功接收到数据
//                        LOG_D("datacenter2 recv data:%s", sock_recv_data_buff);
//                        rs232_uart_write(sock_recv_data_buff, recv_data_size);
//                        rs485_uart_write(sock_recv_data_buff, recv_data_size);
//                    }
//                    else if (recv_data_size == 0)
//                    {
//                        LOG_D("datacenter2 recv data 0 bit.");
//                        LOG_I("datacenter2 disconnect.");
//                        dn400_parm.dn400_status.transparent_transmission_server2_connect_flag = 0;
//                    }
//                    else
//                    {
//                        LOG_D("recv data error!");
//                        LOG_I("datacenter2 disconnect.");
//                        dn400_parm.dn400_status.transparent_transmission_server2_connect_flag = 0;
//                    }
//                }
            }
        }
    }
}

//socket数据异步接收线程3，对应透传3接口
void socket_recv_thread3_entry(void)
{
    int recv_data_size = 0;

    //网络传输模式2或者网络传输模式3
    if (strstr(dn400_parm.dn400_configure.data_transmission_mode, "2") != NULL)
    {
        while (1)
        {
            rt_thread_mdelay(100);
            LOG_D("***************recv data***************");
            if (dn400_parm.dn400_status.private_server_connect_flag == 1)
            {
                memset(sock_recv_data_buff, 0, sizeof(sock_recv_data_buff));
                recv_data_size = tcpclient_recv_data(dn400_parm.dn400_status.private_transmission_sockfd,
                        sock_recv_data_buff, sizeof(sock_recv_data_buff));
                if (recv_data_size > 0)
                {
                    //成功接收到数据
                    LOG_D("接收到数据中心3数据：%s", sock_recv_data_buff);
                    //
                }
                else if (recv_data_size == 0)
                {
                    LOG_D("接收到0位数据中心3数据");
                    dn400_parm.dn400_status.private_server_connect_flag = 0;
                }
                else
                {
                    LOG_D("接收错误");
                    dn400_parm.dn400_status.private_server_connect_flag = 0;
                }
            }
        }
    }
    if (strstr(dn400_parm.dn400_configure.data_transmission_mode, "3") != NULL)
    {
        while (1)
        {
            rt_thread_mdelay(100);
            if (dn400_parm.dn400_status.private_server_connect_flag == 1)
            {
                memset(sock_recv_data_buff, 0, sizeof(sock_recv_data_buff));
                recv_data_size = tcpclient_recv_data(dn400_parm.dn400_status.private_transmission_sockfd,
                        sock_recv_data_buff, sizeof(sock_recv_data_buff));
                if (recv_data_size > 0)
                {
                    //成功接收到数据
                    LOG_D("接收到数据中心3数据：%s", sock_recv_data_buff);
                }
                else if (recv_data_size == 0)
                {
                    LOG_D("接收到0位数据中心3数据");
                    dn400_parm.dn400_status.private_server_connect_flag = 0;
                }
                else
                {
                    LOG_D("接收错误");
                    dn400_parm.dn400_status.private_server_connect_flag = 0;
                }
            }
        }
    }
}

